//
//  ZYCElectiveSelectStudentView.m
//  ZDSC
//
//  Created by ZDTX on 2021/8/16.
//

#import "ZYCElectiveSelectStudentView.h"
#import "ZYCElectiveSelectStudentCell.h"
#import "ZYCCourseStudentModel.h"
@interface ZYCElectiveSelectStudentView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;

@end
@implementation ZYCElectiveSelectStudentView
- (void)reload{
    [self.collectionView reloadData];
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
    return self;
}


#pragma mark - UICollectionViewDelegate && UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ZYCElectiveSelectStudentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ZYCElectiveSelectStudentCell class]) forIndexPath:indexPath];
    ZYCCourseStudentModel *model = self.dataSource[indexPath.item];
    cell.model = model;
    if (model.selected) {
        cell.startImageView.image = [UIImage imageNamed:@"xx"];
    }else{
        cell.startImageView.image = [UIImage imageNamed:@"x-1"];
    }
    if (indexPath.item % 2) {
        cell.bgImageView.image = [UIImage imageNamed:@"czb"];
    }else{
        cell.bgImageView.image = [UIImage imageNamed:@"xxb"];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    for (ZYCCourseStudentModel *model in self.dataSource) {
        model.selected = NO;
    }
    ZYCCourseStudentModel *model = self.dataSource[indexPath.item];
    model.selected = YES;
    if ([self.delegate respondsToSelector:@selector(electiveSelectStudentViewDidClick:)]) {
        [self.delegate electiveSelectStudentViewDidClick:model];
    }
    [self.collectionView reloadData];
}


- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [_collectionView registerClass:[ZYCElectiveSelectStudentCell class] forCellWithReuseIdentifier:NSStringFromClass([ZYCElectiveSelectStudentCell class])];
    }
    return _collectionView;
}

- (UICollectionViewFlowLayout *)flowLayout{
    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc]init];
        _flowLayout.itemSize = CGSizeMake(343, 162);
        _flowLayout.minimumLineSpacing = 25;
//        _flowLayout.minimumInteritemSpacing = kWidth(55);

    }
    return _flowLayout;
}


@end
